const { contextBridge, ipcRenderer  } = require('electron')
var a = 1

// 从渲染器到主进程单向: send
// 从渲染器到主进程单向,并等待结果: sendSync, 
// 从渲染器到主进程双向向: invoke
// 主进程到渲染器进程单向: mainWindow.webContents.send('update-counter', -1),

ipcRenderer.on('asynchronous-reply', (_event, arg) => {
    console.log(arg) // 在 DevTools 控制台中打印“pong”
})
ipcRenderer.send('asynchronous-message', 'ping')

contextBridge.exposeInMainWorld('versions', {
    node: () => process.versions.node,
    chrome: () => process.versions.chrome,
    electron: () => process.versions.electron,
    ping: () => ipcRenderer.invoke('ping'),
    abc: a
})

contextBridge.exposeInMainWorld('ab', 133)

contextBridge.exposeInMainWorld('companyApi', {
    add: (company) => ipcRenderer.invoke('company:add', company),
    delete: (companyId) => ipcRenderer.invoke('company:delete', companyId),
    update: (company) => ipcRenderer.invoke('company:update', company),
    get: (companyId) => ipcRenderer.invoke('company:get', companyId),
    getAll: () => ipcRenderer.invoke('company:getAll'),
})

contextBridge.exposeInMainWorld('projectApi', {
    add: (company) => ipcRenderer.invoke('project:add', company),
    delete: (companyId) => ipcRenderer.invoke('project:delete', companyId),
    update: (company) => ipcRenderer.invoke('project:update', company),
    get: (companyId) => ipcRenderer.invoke('project:get', companyId),
    getAll: () => ipcRenderer.invoke('project:getAll'),
})

contextBridge.exposeInMainWorld('itemApi', {
    add: (company) => ipcRenderer.invoke('item:add', company),
    delete: (companyId) => ipcRenderer.invoke('item:delete', companyId),
    update: (company) => ipcRenderer.invoke('item:update', company),
    get: (companyId) => ipcRenderer.invoke('item:get', companyId),
    getAll: () => ipcRenderer.invoke('item:getAll'),
})

contextBridge.exposeInMainWorld('commonApi', {
    openLink: (link) => ipcRenderer.invoke('common:openLink', link),
    onUpdateRoute: (callback) => ipcRenderer.on('update-route', (_event, value) => callback(value)),
})

contextBridge.exposeInMainWorld('userApi', {
    get: () => ipcRenderer.invoke('user:get'),
    update: (arg) => ipcRenderer.invoke('user:update', arg),
})

contextBridge.exposeInMainWorld('bookmarkApi', {
    add: (company) => ipcRenderer.invoke('bookmark:add', company),
    delete: (companyId) => ipcRenderer.invoke('bookmark:delete', companyId),
    update: (company) => ipcRenderer.invoke('bookmark:update', company),
    get: (companyId) => ipcRenderer.invoke('bookmark:get', companyId),
    getAll: () => ipcRenderer.invoke('bookmark:getAll'),
})

contextBridge.exposeInMainWorld('dbApi', {
    save: () => ipcRenderer.invoke('db:save'),
})

contextBridge.exposeInMainWorld('configApi', {
    get: () => ipcRenderer.invoke('config:get'),
})